﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SPListTemplateManager {
    public class Helpers {

        public static List<SPWebApplication> webAppsList = new List<SPWebApplication>();

        /// <summary>
        /// Get all web applications
        /// </summary>
        /// <param name="isRefresh">if true - refresh immediately (get web apps), if false - first check if list has items</param>
        /// <returns></returns>
        public static void GetAllWebApplications(bool isRefresh) {
            try {
                if (!isRefresh) {
                    if (webAppsList.Count == 0)
                        GetAllWebApplications(true);
                } else {
                    SPServiceCollection services = SPFarm.Local.Services;
                    foreach (SPService curService in services) {
                        if (curService is SPWebService) {
                            SPWebService webService = (SPWebService)curService;
                            foreach (SPWebApplication webApp in webService.WebApplications) {
                                webAppsList.Add(webApp);
                            }
                        }
                    }
                }                
            } catch (Exception e) {
                MessageBox.Show("ERROR: " + e.Message);                
            }
            
        }


        /// <summary>
        /// Fill ComboBox with items of type SPWebApplication
        /// </summary>
        /// <param name="comboBox"></param>
        public static void FillWebAppComboBox(ComboBox comboBox) {
            comboBox.Items.Clear();
            foreach (SPWebApplication webApp in Helpers.webAppsList) {
                comboBox.Items.Add(webApp);
            }
        }

        /// <summary>
        /// Gets string value from list item
        /// </summary>
        /// <param name="item"></param>
        /// <param name="fieldName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetSPFieldStringValue(SPListItem item, string fieldName, string defaultValue) {
            try {
                return item[fieldName].ToString();                
            } catch (Exception) {
                return defaultValue;
            }
        }


        /// <summary>
        /// Gets int value from list item
        /// </summary>
        /// <param name="item"></param>
        /// <param name="fieldName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int GetSPFieldIntValue(SPListItem item, string fieldName, int defaultValue) {
            try {
                return int.Parse(item[fieldName].ToString());
            } catch (Exception) {
                return defaultValue;
            }
        }
    }
}
